/*
 * Copyright 2010, 2011 Matt Oliveri
 *
 * This file is part of JConfigGen
 *
 * JConfigGen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JConfigGen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with JConfigGen.  If not, see <http://www.gnu.org/licenses/>.
 */

package atma.jconfiggen.sax;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import atma.jconfiggen.Expr;
import atma.jconfiggen.Seq;
import atma.jconfiggen.Proxy;

class ExpListH extends ElemH
{
	private static final AttrMatcher snewAttr = new AttrMatcher("(new)","types","id","args");
	private static final AttrMatcher scallAttr = new AttrMatcher("(call)","type","id","args");

	ArrayList<Expr> sub;

	ExpListH(SAXHandler s) {super(s);}
	ExpListH(SAXHandler s,ArrayList<Expr> e) {super(s);sub = e;}

	final void setupWith(ArrayList<Expr> pnt,Expr obj)
	{
		pnt.add(new Seq(sub));
		Proxy p = new Proxy(obj);
		sub.add(p);
		saxh.withs.push(p);
	}

	final NewH snew(String tag,Attributes attr) throws SAXException
	{
		AttrMatcher.Match atm = snewAttr.matchExt(saxh.loc,attr);
		String[] atv = atm.match;
		return HFact.mkNew(saxh,sub,tag,atv[0],atv[1],atv[2],atm.extc,atm.extv,attr);
	}

	final ElemH scall(String tag,Attributes attr) throws SAXException
	{
		String[] atv = scallAttr.match(saxh.loc,attr);
		return HFact.mkCall(saxh,sub,atv[0],atv[1],tag,atv[2]);
	}

	ElemH elem(SAXHandler.Elem e,Attributes attr) throws SAXException
	{
		HFact f = saxh.getFactory(e);
		if (f == null) return super.elem(e,attr);
		return f.make(sub,attr);
	}

	ElemH elemstr(String tag,Attributes attr) throws SAXException {return snew(tag,attr);}
}
